package com.guobox.movies.view.mview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.guobox.movies.R;


/**
 * Created by xxl on 16/7/14.
 */
public class PlayButton extends View {
    private static final String TAG = "PLAYTEST";
    private int mainColor,subColor;
    private int left,top,right,bottom;
    Paint paint;
    Paint triPaint;
    Path tri = new Path();
    public PlayButton(Context context) {
        super(context);
    }

    public PlayButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        triPaint = new Paint();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs
                , R.styleable.PlayButton, 0, 0);
        mainColor = typedArray.getColor(R.styleable.PlayButton_main_color, Color.BLACK);
        subColor = typedArray.getColor(R.styleable.PlayButton_sub_color, Color.WHITE);
        typedArray.recycle();
    }

    public PlayButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs
                , R.styleable.PlayButton, defStyleAttr,0 );
        mainColor = typedArray.getColor(R.styleable.PlayButton_main_color, Color.BLACK);
        subColor = typedArray.getColor(R.styleable.PlayButton_sub_color, Color.WHITE);
        typedArray.recycle();
    }

    public PlayButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        paint = new Paint();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs
                , R.styleable.PlayButton, defStyleAttr,defStyleRes );
        mainColor = typedArray.getColor(R.styleable.PlayButton_main_color, Color.BLACK);
        subColor = typedArray.getColor(R.styleable.PlayButton_sub_color, Color.WHITE);
        typedArray.recycle();
    }

    public int getMainColor() {
        return mainColor;
    }

    public void setMainColor(int mainColor) {
        this.mainColor = mainColor;
        invalidate();
        requestLayout();
    }

    public int getSubColor() {
        return subColor;
    }

    public void setSubColor(int subColor) {
        this.subColor = subColor;
        invalidate();
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        paint.setColor(mainColor);
//        Log.i(TAG, left + "*" + top + "*" + right + "*" + bottom);
//        Log.i(TAG, getWidth() + "*" + getHeight());
        paint.setAntiAlias(true);
        paint.setColor(mainColor);
        triPaint.setAntiAlias(true);
        canvas.drawRoundRect(0,0,getWidth(),getHeight(),7,7,paint);
        triPaint.setStyle(Paint.Style.FILL);
        triPaint.setColor(subColor);
        tri.moveTo(58,21);
        tri.lineTo(58,59);
        tri.lineTo(88,40);
        tri.close();
        canvas.drawPath(tri,triPaint);
    }
}
